#!/bin/sh

# compares with rdbg -lurette the results of:
#   lus2lic -ec and ecexe
#   lus2lic -exec

eval `opam config env`
ocamlopt=ocamlopt.opt
lustre_file=$1
shift
OPT=$@

node=`basename $lustre_file .lus`
_oracle=_"$node"_oracle.lus
oracle="$node"_oracle
lv4="$node"_lv4.lus
ec="$node".ec
lv4_node="$node__$node"
env=_"$node"_env.lut
export RDBG_PATH="$HOME/rdbg"

set -x verbose #echo on
set -e # stop at the first error!

if
    [ -f $env_node ];
then
   echo "$env_node generated"

else if
     ./lus2lic $OPT $lustre_file -n $node --gen-autotest -np &&
     ./lus2lic $OPT $lustre_file -n $node -np -lic -o $lustre_file.lic &&
     cat $lustre_file.lic  >> $_oracle;
then
     echo "./lus2lic $OPT $lustre_file -n $node --gen-autotest -np: ok"
else
    echo "Error"
    exit 2
fi
fi


if
    ./lus2lic $lustre_file -n $node  -ec  -o $ec;
#    ./lus2lic $lustre_file -n $node  -en -lv4 -eei --no-prefix -o $lv4;
then
    echo "lus2lic -ec done"
else
    echo "Error"
    exit 2
fi


export PATH=/usr/local/tools/lustre/v4/bin/:$PATH
#  if
#      ./lus2lic $_oracle -n $oracle  -ec -o $oracle.ec
#  #    ./lus2lic  lv4$_oracle -n $oracle -ec -o $oracle.ec
#  #    lus2ec  $_oracle  $oracle  -o $oracle.ec;
#  then
#  echo "ec generation done"
#  else
#      echo "Error"
#      exit 2
#  fi


export CMXA="polka.cmxa bdd.cmxa lut4ocaml.cmxa"

#  ./lurettetop -p 6  -seed 42 \
#      -rp "sut:ec:$ec:$lv4_node" \
#      -rp "env:lutin:$env" \
#      -rp "oracle:v6:$_oracle:$oracle" \
#      -l 10;
#--stop-on-oracle-error;

msg="An error occured in $0. Aborting."

echo "Generating oracle.cmxs"
./lus2lic  $OPT -ocaml -o oracle.ml --expand-io-type $_oracle -n $oracle
$ocamlopt -shared -o  oracle.cmxs -I +rdbg-plugin -I +lustre-v6 lustre-v6.cmxa  oracle.ml

echo "Generating env.cmxs"
PRECISION=4

lutin -p $PRECISION -ocaml -o env.ml $env
$ocamlopt -shared -o env.cmxs -I +rdbg-plugin -I +lutin    $CMXA env.ml

echo "oracle.cmxs and env.cmxs are generated."
# $RDBG_PATH/rdbgbatch.native

eval "./rdbg-batch  -lurette -l 10 -p $PRECISION --stop-on-oracle-error  --sut-stdio  \"./patch_ecexe $ec $lv4_node\"  --env env.cmxs      --oracle oracle.cmxs "

#  echo "  $RDBG_PATH/rdbgbatch.native -lurette --seed 42 -l 100 -p 6 --stop-on-oracle-error \
#            --sut-stdio  \"./patch_ecexe $ec $lv4_node\"  \
#            --env-stdio \"./lutin -boot -rif $env\" \
#            --oracle-stdio \"./lus2lic -exec -rif $_oracle -n $oracle\"  ";
#        --sut sut.cmxs \
#          --env-stdio "./lutin -boot -rif $env" \
#          --oracle-stdio "./lus2lic -exec -rif $_oracle -n $oracle"  ;


echo "rdbg -lurette: ok"
exit 0
